//
// Created by Administrator on 2024/9/10.
//

#include "Display.h"
#include "aiKuaiApi/aikuaiApi.h"
#include "sysSet.h"


extern String loadCookie;
extern sys_set this_dev_set;

void setDisLum(uint8_t val)//设置屏幕背光亮度
{
    ledcWrite(0, map(val, 0, 100, 0, 255));
}

void flashDis(void *)
{
    pinMode(5, OUTPUT);
    ledcSetup(0, 5000, 8);  // 配置通道0，频率5kHz，8位分辨率
    ledcAttachPin(5, 0);  // 将GPIO 32关联到通道0
    setDisLum(30);
    uint32_t up_down_data[2][2][10] = {};//[u/d][use/backups][num],[0]up,[1]down

    uint32_t data_num = 0;
    vTaskDelay(3000);//等待lvgl框架加载完成
    while (1)
    {
        while (loadCookie == "")
        {
            vTaskDelay(1000);
        }


        sysStater st;
        st = getSysRunStater(loadCookie);
        if (st.err_code != 200)
        {
            ESP_LOGD("re", "数据解析失败");
            loadCookie = getLoadCookie("http://yu.vip7x24.com:81/Action/login", "test", "test123");
        } else
        {
            ESP_LOGD("re", "数据解析成功");
            ESP_LOGD("re", "%.2f", st.CPU_user);
            //刷新屏幕
            //计算运行的天
            uint8_t run_day = 0, run_hour = 0, run_minute = 0;
            //判断天数

            if (st.sys_run_day > 60)
            {

                if (st.sys_run_day > 3600)
                {
                    if (st.sys_run_day > 86400)
                    {
                        run_day = st.sys_run_day / 86400;
                    }
                    run_hour = (st.sys_run_day - run_day * 86400) / 3600;
                }
                run_minute = (st.sys_run_day - (run_day * 86400) - (run_hour * 3600)) / 60;
            }

            ESP_LOGD("run_time", "run_day=%d,run_hour=%d,run_second=%d", run_day, run_hour, run_minute);
            String t = "已运行";
            t += run_day;
            t += "天";
            t += run_hour;
            t += "小时";
            t += run_minute;
            t += "分";
            lv_label_set_text(ui_Label14, t.c_str());

            //更新温度,内存,cpu数据
            lv_label_set_text(ui_Label13, ((String) st.CPU_temp).c_str());
            lv_label_set_text(ui_Label31, ((String) st.memory_user).c_str());
            lv_label_set_text(ui_Label12, ((String) st.CPU_user).c_str());
            //移动对应的进度条
            lv_obj_set_x(ui_Image28, map(st.CPU_temp, 20, 100, -61, 0));
            lv_obj_set_x(ui_Image30, map(st.memory_user, 0, 100, -61, 0));
            lv_obj_set_x(ui_Image33, map(st.CPU_user, 0, 100, -61, 0));
            //更新图表
            //更新y坐标
            //判断坐标类型
            lv_chart_series_t *ui_Chart1_series_1 = lv_chart_add_series(ui_Chart1, lv_color_hex(0x2941CE),
                                                                        LV_CHART_AXIS_SECONDARY_Y);
            lv_chart_series_t *ui_Chart1_series_2 = lv_chart_add_series(ui_Chart1, lv_color_hex(0xF11616),
                                                                        LV_CHART_AXIS_SECONDARY_Y);
            st.upload /= 1000;
            st.down_load /= 1000;
            ESP_LOGD("l1", "up=%d,down=%d", st.upload, st.down_load);
            if (data_num < 10)
            {
                up_down_data[0][0][data_num] = st.upload;
                up_down_data[1][0][data_num] = st.down_load;
                ESP_LOGD("l1", "i=%d,up=%d,down=%d", data_num, st.upload,
                         st.down_load);
                ESP_LOGD("l1", "i=%d,up=%d,down=%d", data_num, up_down_data[0][0][data_num],
                         up_down_data[1][0][data_num]);
                for (int i = 0; i < data_num; i++)
                {
                    ESP_LOGD("l1", "i=%d,up=%d,down=%d", data_num, up_down_data[0][0][i],
                             up_down_data[1][0][i]);
                }
            } else
            {
                for (int i = 0; i < 9; i++)
                {
                    //原始数组向前推一位
                    up_down_data[0][1][i] = up_down_data[0][0][i + 1];
                    up_down_data[1][1][i] = up_down_data[1][0][i + 1];
                }
                up_down_data[0][1][9] = st.upload;  //备用数组最后一位为当前的的数据
                up_down_data[1][1][9] = st.down_load;

                for (int i = 0; i < 10; i++)
                {
                    up_down_data[0][0][i] = up_down_data[0][1][i];//将备用数组拷贝到原数组
                    up_down_data[1][0][i] = up_down_data[1][1][i];
                    ESP_LOGD("l1", "up=%d,down=%d", up_down_data[0][0][i], up_down_data[1][0][i]);
                }
            }
/*
            //先检查所有数据来匹配对应的图表
            uint8_t now_speed_net = 0;//0kb,1mb,2gb
            if (data_num<10)
            {
                for (int i = 0; i < data_num; i++)
                {

                    ESP_LOGD("l8", "up=%d,down=%d", up_down_data[0][0][i], up_down_data[1][0][i]);
                    if ((up_down_data[0][0][i] > 1000 && up_down_data[0][0][i] < 1000000) ||
                        (up_down_data[1][0][i] > 1000 && up_down_data[1][0][i] < 1000000))
                    {
                        now_speed_net = 1;
                        break;
                    }

                    if (up_down_data[0][0][i] > 1000000 || up_down_data[1][0][i] > 1000000)
                    {
                        now_speed_net = 2;
                        break;
                    }
                    if (up_down_data[0][0][i] <= 1000 && up_down_data[1][0][i] <= 1000)
                    {
                        now_speed_net = 0;
                    }
                }
            } else
            {
                for (int i = 0; i < 10; i++)
                {

                    ESP_LOGD("l8", "up=%d,down=%d", up_down_data[0][0][i], up_down_data[1][0][i]);
                    if ((up_down_data[0][0][i] > 1000 && up_down_data[0][0][i] < 1000000) &&
                        (up_down_data[1][0][i] > 1000 && up_down_data[1][0][i] < 1000000))
                    {
                        now_speed_net = 1;
                        break;
                    }

                    if (up_down_data[0][0][i] > 1000000 && up_down_data[1][0][i] > 1000000)
                    {
                        now_speed_net = 2;
                        break;
                    }
                    if (up_down_data[0][0][i] <= 1000 && up_down_data[1][0][i] <= 1000)
                    {
                        now_speed_net = 0;
                    }
                }
            }

            //转换数据

            for (int i = 0; i < 10; i++)
            {
                if (now_speed_net == 0)
                {
                    up_down_data[0][0][i] = up_down_data[0][1][i];
                    up_down_data[1][0][i] = up_down_data[1][1][i];
                } else if (now_speed_net == 1)
                {
                    up_down_data[0][0][i] = up_down_data[0][1][i] / 1000;
                    up_down_data[1][0][i] = up_down_data[1][1][i] / 1000;
                } else if (now_speed_net == 2)
                {
                    up_down_data[0][0][i] = up_down_data[0][1][i] / 1000000;
                    up_down_data[1][0][i] = up_down_data[1][1][i] / 1000000;
                }
            }
*/

            //转换数据(下载分辨率1M,上传分辨率100k)
            if (data_num < 10)
            {
                up_down_data[0][0][data_num] /= 100;
                up_down_data[1][0][data_num] /= 1000;
            } else
            {
                up_down_data[0][0][9] /= 100;
                up_down_data[1][0][9] /= 1000;
            }

            {
                uint8_t _s = 0, _b = 0, _q = 0;
                for (int i = 0; i < 10; i++)
                {
                    if (up_down_data[0][0][i] < 10 && up_down_data[1][0][i] < 10)
                    {
                        _s++;
                    } else if ((up_down_data[0][0][i] > 10 && up_down_data[0][0][i] < 100) ||
                               (up_down_data[1][0][i] > 10 && up_down_data[1][0][i] < 100))
                    {
                        _b++;
                    } else if ((up_down_data[0][0][i] > 100 && up_down_data[0][0][i] < 1000) ||
                               (up_down_data[1][0][i] > 100 && up_down_data[1][0][i] < 1000))
                    {
                        _q++;
                    }

                    if (_q > 0)
                    {
                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_PRIMARY_Y, 0, 1000);
                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_SECONDARY_Y, 0, 1000);
                    } else if (_b > 0)
                    {
                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_SECONDARY_Y, 0, 100);
                    } else
                    {
                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_PRIMARY_Y, 0, 10);
                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_SECONDARY_Y, 0, 10);
                    }

//                    if (i == 10)
//                    {
//                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
//                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_SECONDARY_Y, 0, 100);
//                        break;
//                    }
//                    if (up_down_data[0][0][i] > 100 && up_down_data[1][0][i] > 100)
//                    {
//                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_PRIMARY_Y, 0, 1000);
//                        lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_SECONDARY_Y, 0, 1000);
//                        break;
//                    }
                }
            }


            lv_coord_t ui_Chart1_series_1_array[10] = {};
            lv_coord_t ui_Chart1_series_2_array[10] = {};
            for (int i = 0; i < 10; i++)
            {
                ui_Chart1_series_1_array[i] = (lv_coord_t) up_down_data[0][0][i];
                ui_Chart1_series_2_array[i] = (lv_coord_t) up_down_data[1][0][i];
                ESP_LOGD("l9", "up=%d,down=%d", up_down_data[0][0][i], up_down_data[1][0][i]);
            }
//             lv_coord_t aaa[] = { (lv_coord_t)lv_rand(0,1000), (lv_coord_t)lv_rand(0,1000), (lv_coord_t)lv_rand(0,1000), 130, 410, 150, 610, 710, 500 };
//            lv_chart_set_all_value(ui_Chart1,ui_Chart1_series_1,LV_CHART_POINT_NONE);
//            lv_chart_set_next_value(ui_Chart1, ui_Chart1_series_1, lv_rand(0,100));
//            for (int i = 0; i < 10; i++)
//            {
//                ui_Chart1_series_1_array[i] = (lv_coord_t) lv_rand(0, 1000);
//            }
            lv_chart_set_ext_y_array(ui_Chart1, ui_Chart1_series_1, ui_Chart1_series_1_array);
            lv_chart_set_ext_y_array(ui_Chart1, ui_Chart1_series_2, ui_Chart1_series_2_array);
            ESP_LOGD("l19", "aaa[0]=%d,aaa[1]=%d", ui_Chart1_series_1_array[0], ui_Chart1_series_1_array[1]);

            lv_chart_refresh(ui_Chart1);

            //更新底部流量数据
            {
                String a;
                if (st.upload > 1000 && st.upload < 1000000)
                {
                    double b = st.upload / 1000.0;
                    a += b;
                    a += "M";
                } else if (st.upload > 1000000)
                {
                    double b = st.upload / 1000000.0;
                    a += b;
                    a += "G";
                } else
                {
                    a += st.upload;
                    a += "K";
                }


                lv_label_set_text(ui_Label18, a.c_str());
            }
            {
                String a;
                if (st.down_load > 1000 && st.down_load < 1000000)
                {
                    double b = st.down_load / 1000.0;
                    a += b;
                    a += "M";
                } else if (st.down_load > 1000000)
                {
                    double b = st.down_load / 1000000.0;
                    a += b;
                    a += "G";
                } else
                {
                    a += st.down_load;
                    a += "K";
                }
                lv_label_set_text(ui_Label20, a.c_str());
            }

            //刷新ip地址
            {
                String a = WiFi.localIP().toString();
                lv_label_set_text(ui_Label16, a.c_str());
            }

        }
        data_num++;
        vTaskDelay(this_dev_set.update_time);
    }
}